package com.xl.basic.controller;

import com.xl.basic.annotation.RoleValidated;
import com.xl.basic.entity.Result;
import com.xl.basic.entity.UserInfo;
import com.xl.basic.entity.param.AddMenuParam;
import com.xl.basic.entity.param.QueryMenuParam;
import com.xl.basic.entity.param.UpdMenuParam;
import com.xl.basic.service.MenuService;
import com.xl.basic.utils.LoginUtils;
import com.xl.dao.domain.XlMenu;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 钟勇
 * Create in 2023/8/31 9:31
 */
@Tag(name = "菜单管理")
@RestController
@RequestMapping("/api/Menu")
@RequiredArgsConstructor
@RoleValidated
public class MenuController {
    private final MenuService menuService;

    @Operation(summary = "新增菜单")
    @PostMapping(value = "/addMenu")
    Result<Integer> addMenu(@Validated AddMenuParam param) {
        Result<UserInfo> userInfoRes = LoginUtils.getUserInfo();
        return menuService.addMenu(param, userInfoRes.getResult());
    }

    @Operation(summary = "编辑菜单")
    @PostMapping(value = "/updMenu")
    Result<Object> updMenu(@Validated UpdMenuParam param) {
        Result<UserInfo> userInfoRes = LoginUtils.getUserInfo();
        return menuService.updMenu(param, userInfoRes.getResult());
    }


    @Operation(summary = "移除菜单")
    @PostMapping(value = "/delMenu")
    Result<Object> delMenu(@NotNull(message = "菜单ID不能为空") Integer menuId) {
        Result<UserInfo> userInfoRes = LoginUtils.getUserInfo();
        return menuService.delMenu(menuId, userInfoRes.getResult());
    }

    @Operation(summary = "查询菜单列表")
    @PostMapping(value = "/queryMenuList")
    Result<List<XlMenu>> queryMenuList(QueryMenuParam param) {
        return menuService.queryMenuList(param);
    }

    @Operation(summary = "查询当前用户菜单列表")
    @PostMapping(value = "/queryCurrUserMenuList")
    Result<List<XlMenu>> queryCurrUserMenuList(Integer roleId) {
        Result<UserInfo> userInfoRes = LoginUtils.getUserInfo();
        if (userInfoRes.isErr()) {
            return userInfoRes.create();
        }

        String userId = userInfoRes.getResult().getUserId();
        return queryMenuList(new QueryMenuParam().setUserId(userId).setRoleId(roleId));
    }


}
